package cn.com.assistant.service.impl;

import cn.com.assistant.entity.PubUserEntity;
import cn.com.assistant.mapper.PubUserMapper;
import cn.com.assistant.model.EditPubUserModel;
import cn.com.assistant.model.PubUserPOJO;
import cn.com.assistant.service.PubUserService;
import cn.com.assistant.utils.ResponseMessage;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class PubUserServiceImpl implements PubUserService {

    @Autowired
    public PubUserMapper pubUserMapper;
 
    @Override
    public List<PubUserEntity> queryAllPubUser() {
        return pubUserMapper.queryAllPubUser();
    }

    @Override
    public ResponseMessage pubUserRegister(PubUserPOJO pubUserPOJO) {
        // 对象数据转换
        PubUserEntity pubUserEntity = new PubUserEntity();
        BeanUtils.copyProperties(pubUserPOJO, pubUserEntity);
        pubUserEntity.setAuditStatus(0);
        pubUserEntity.setCreatedDate(new Date());
        int result = pubUserMapper.insert(pubUserEntity);
        if (result == 1) {
            return ResponseMessage.defaultResponse().setMsg("注册成功！");
        } else {
            return ResponseMessage.failResponse().setMsg("注册失败！");
        }
    }

    @Override
    public ResponseMessage pubUserLogin(String username, String password) {
        PubUserEntity entity = pubUserMapper.findByUsername(username);
        if (entity==null || username == null) {
            return ResponseMessage.failResponse().setMsg("登录失败！");
        } else if (entity.getAuditStatus() == 0 ) {
            return ResponseMessage.failResponse().setMsg("注册信息待审核！");
        } else if (entity.getAuditStatus() == 2 ) {
            return ResponseMessage.failResponse().setMsg("注册信息审核未通过！");
        } else if (password.equals(entity.getPassword())){
            entity.setPassword("");
            return ResponseMessage.defaultResponse().setData(entity).setMsg("登录成功！");
        }
        return ResponseMessage.failResponse().setMsg("登录失败！");
    }

    @Override
    public ResponseMessage existByUsername(String username, String id) {
        ResponseMessage responseMessage = ResponseMessage.defaultResponse();
        List<PubUserEntity> pubUserEntities = pubUserMapper.findByUsernameAndIdNot(username, id);
        if (null != pubUserEntities && pubUserEntities.size() > 0) {
            responseMessage.setStatus(0).setMsg("该注册账号已经存在！");
        }
        return responseMessage;
    }

    @Override
    public ResponseMessage findByPhoneAndIdNot(String phone, String id) {
        ResponseMessage responseMessage = ResponseMessage.defaultResponse();
        List<PubUserEntity> publicUserEntities = pubUserMapper.findByPhoneAndIdNot(phone, id);
        if (null != publicUserEntities && publicUserEntities.size() > 0) {
            responseMessage.setStatus(0).setMsg("该手机号码已经绑定系统账号，请重新输入");
        }
        return responseMessage;
    }

    @Override
    public PubUserEntity findByUsername(String username) {
        return pubUserMapper.findByUsername(username);
    }

    @Override
    public ResponseMessage updatePassword(EditPubUserModel editPubUserModel) {
        // 校验原密码是否正确
        PubUserEntity entity = pubUserMapper.findByUsername(editPubUserModel.getUsername());
        if (!entity.getPassword().equals(editPubUserModel.getOldPwd())) {
            return ResponseMessage.validFailResponse().setMsg("旧密码不正确，请重新输入！");
        }
        entity.setPassword(editPubUserModel.getPassword());
        entity.setUpdatedDate(new Date());
        pubUserMapper.updateByUsername(entity);
        return ResponseMessage.defaultResponse().setMsg("密码修改成功！");
    }

    @Override
    public ResponseMessage examine(Integer auditStatus, String auditOpinion, Integer id) {
        PubUserEntity pubUserEntity = pubUserMapper.findById(id);
        if(pubUserEntity == null){
            return ResponseMessage.failResponse().setMsg("用户信息不存在！");
        }
        pubUserEntity.setAuditStatus(auditStatus);
        pubUserEntity.setAuditOpinion(auditOpinion);
        pubUserEntity.setUpdatedDate(new Date());
        int num = pubUserMapper.updateByUsername(pubUserEntity);
        pubUserEntity.setPassword("");
        return num > 0 ? ResponseMessage.defaultResponse().setMsg("审核成功").setData(pubUserEntity) : ResponseMessage.failResponse().setMsg("审核失败");
    }

    @Override
    public ResponseMessage findByPage(Integer page, Integer size, Map<String, Object> filter) {

        return ResponseMessage.defaultResponse();
    }
}